#include "qprojectsettingwidget.h"
#include "ui_qprojectsettingwidget.h"

#include "../../lib/corelib/stylehelper.h"
#include "../../lib/shared/qproject.h"
#include "../../include/abstractcore.h"

#include <QPainter>
#include <QLinearGradient>

QProjectSettingWidget::QProjectSettingWidget(AbstractCore *core, QWidget *parent):
    QWidget(parent),
    m_core(core),
    ui(new Ui::QProjectSettingWidget)
{
    ui->setupUi(this);

    connect(QProject::project(),SIGNAL(openProject()),this,SLOT(openProject()));
    connect(QProject::project(),SIGNAL(beforeClosed()),this,SLOT(projectClosed()));
    connect(QProject::project(),SIGNAL(changed(int,int)),this,SLOT(changed(int,int)));
}

QProjectSettingWidget::~QProjectSettingWidget()
{
    delete ui;
}

void QProjectSettingWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);


    p.setPen(StyleHelper::borderColor());

    QLinearGradient grad(this->rect().topLeft(), rect().bottomLeft());
    grad.setColorAt(0, QColor(240,240,240));
    grad.setColorAt(0.3, QColor(220,220,220));
    grad.setColorAt(0.6, QColor(210,210,210));
    grad.setColorAt(1, QColor(240,240,240));
    p.fillRect(rect().adjusted(1,1,-1,-1), grad);


    p.drawRect(this->rect().adjusted(0,0,-1,-1));
}

void QProjectSettingWidget::openProject()
{
    disconnect(ui->first_page,SIGNAL(currentIndexChanged(QString)),this,SLOT(firstPage(QString)));
    QString path=QProject::project()->getProjectPath();

    ui->first_page->clear();

    ui->pro_name->setEnabled(true);
    ui->pro_path->setEnabled(true);
    ui->des_size_hei->setEnabled(true);
    ui->des_size_wid->setEnabled(true);
    ui->run_size_hei->setEnabled(true);
    ui->run_size_wid->setEnabled(true);
    ui->first_page->setEnabled(true);

    ui->pro_name->setText(QProject::project()->getProjectName());
    ui->pro_path->setText(path);
    ui->des_size_wid->setValue(QProject::project()->getDesignSize().width());
    ui->des_size_hei->setValue(QProject::project()->getDesignSize().height());
    ui->run_size_hei->setValue(QProject::project()->getRunningSize().height());
    ui->run_size_wid->setValue(QProject::project()->getRunningSize().width());

    QProject *p=QProject::project();
    for(int i=0;i<p->getPageCount();i++)
    {
        ui->first_page->addItem(p->getPage(i).m_name);
    }

    if(ui->first_page->count()>0)
    {
        int index=ui->first_page->findText(p->getFirstPage());
        if(index<0 || index>=ui->first_page->count())
        {
            index=0;
            p->setFirstPage(p->getPage(0).m_name);
        }
        ui->first_page->setCurrentIndex(ui->first_page->findText(p->getFirstPage()));
    }
    else
    {
        p->setFirstPage("");
    }

    connect(ui->first_page,SIGNAL(currentIndexChanged(QString)),this,SLOT(firstPage(QString)));
}

void QProjectSettingWidget::projectClosed()
{
    ui->pro_name->setEnabled(false);
    ui->pro_path->setEnabled(false);
    ui->des_size_hei->setEnabled(false);
    ui->des_size_wid->setEnabled(false);
    ui->run_size_hei->setEnabled(false);
    ui->run_size_wid->setEnabled(false);
    ui->first_page->setEnabled(false);

    ui->pro_name->setText("");
    ui->pro_path->setText("");
    ui->des_size_wid->setValue(1);
    ui->des_size_hei->setValue(1);
    ui->run_size_hei->setValue(1);
    ui->run_size_wid->setValue(1);
    ui->first_page->clear();
    disconnect(ui->first_page,SIGNAL(currentIndexChanged(QString)),this,SLOT(firstPage(QString)));
}

void QProjectSettingWidget::changed(int type,int index)
{
    if(type==PCT_PAGE_DATA_ADD)
    {
        ui->first_page->insertItem(index,QProject::project()->getPage(index).m_name);
        ui->first_page->setCurrentIndex(ui->first_page->findText(QProject::project()->getFirstPage()));
    }
    else if(type==PCT_PAGE_DATA_DEL)
    {
        ui->first_page->removeItem(index);
        QString str=QProject::project()->getFirstPage();
        if(str=="")
        {
            ui->first_page->setCurrentIndex(-1);
        }
        else
        {
            ui->first_page->setCurrentIndex(ui->first_page->findText(str));
        }
    }

}

void QProjectSettingWidget::firstPage(const QString &arg1)
{
    QProject::project()->setFirstPage(ui->first_page->currentText());
}

void QProjectSettingWidget::on_des_size_wid_valueChanged(int arg1)
{
    QSize sz=QProject::project()->getDesignSize();
    sz.setWidth(arg1);
    QProject::project()->setDesignSize(sz);
}

void QProjectSettingWidget::on_des_size_hei_valueChanged(int arg1)
{
    QSize sz=QProject::project()->getDesignSize();
    sz.setHeight(arg1);
    QProject::project()->setDesignSize(sz);
}

void QProjectSettingWidget::on_run_size_wid_valueChanged(int arg1)
{
    QSize sz=QProject::project()->getRunningSize();
    sz.setWidth(arg1);
    QProject::project()->setRunningSize(sz);
}

void QProjectSettingWidget::on_run_size_hei_valueChanged(int arg1)
{
    QSize sz=QProject::project()->getRunningSize();
    sz.setHeight(arg1);
    QProject::project()->setRunningSize(sz);
}
